package com.qf.shop.controller;

import com.qf.shop.annotation.ProcessInfo;
import com.qf.shop.entity.Goods;
import com.qf.shop.entity.OrderAddress;
import com.qf.shop.entity.Orders;
import com.qf.shop.model.Results;
import com.qf.shop.mapper.OrdAddressMapper;
import com.qf.shop.mapper.OrdersMapper;
import com.qf.shop.model.TableData;
import com.qf.shop.pojo.QueryOrder;
import com.qf.shop.service.IOrdersService;
import com.qf.shop.vo.ChooseAddress;
import com.qf.shop.vo.OrdAddreeSon;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/order")
public class OrdersController {


    private IOrdersService OrdersService;

    public OrdersController(IOrdersService ordersService) {
        this.OrdersService = ordersService;
    }

    @GetMapping
    @ProcessInfo("查询所有订单")
    public TableData<Orders> getAllGoods(QueryOrder queryOrder,
                                        @RequestParam(defaultValue = "0") Integer offset,
                                        @RequestParam(defaultValue = "10") Integer limit){

        TableData<Orders> allOrders = OrdersService.getAllOrders(queryOrder,offset,limit);

        return allOrders;

    }

    // 根据Id查询
    @GetMapping("/{id}")
    @ProcessInfo("查询id查询订单")
    public Orders getUserById(@PathVariable("id") Integer id) {
        Orders orderUserById = OrdersService.getOrderUserById(id);
        return orderUserById;
    }

    // 根据uId查询
    @GetMapping("/uid/{id}")
    @ProcessInfo("查询可选的收获地址")
    public List<ChooseAddress> getChooseAddress(@PathVariable("id") Integer id) {
        List<ChooseAddress> chooseAddress = OrdersService.getChooseAddress(id);
        return chooseAddress;
    }

    //根据uid,aid改变地址
    @GetMapping("/changeOrder/{aid}/{uid}")
    @ProcessInfo("改变收获地址")
    public Results changeAddress(@PathVariable("aid") Integer aid,@PathVariable("uid") Integer uid){

        Results results = new Results();

        try{
            OrdersService.changeAddress(aid, uid);
            results.setCode(1);
            results.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            results.setCode(-1);
            results.setMsg("fail");
        }
        return results;


    }


    @GetMapping("show/{id}")
    @ProcessInfo("展示购物车地址")
    public List<OrdAddreeSon> showTakeDeliveryAddress(@PathVariable("id") Integer id) {
        List<OrdAddreeSon> list = OrdersService.showTakeDeliveryAddress(id);
        list.forEach(orderAddress -> {
            System.out.println(orderAddress);
        });
      return list;
    }

    //争议代码
//    @GetMapping("show/{id}")
//    public List<OrderAddress> showTakeDeliveryAddress(@PathVariable("id") Integer id) {
//        System.out.println(id+"========================");
//        List<OrderAddress> list = OrdersService.showTakeDeliveryAddress(id);
//        list.forEach(orderAddress -> {
//            System.out.println(orderAddress);
//        });
//        return list;
//    }

}
